import Vue from 'vue'
import Router from 'vue-router'
import Home from './views/Home.vue'
import HttpRequest from './libs/axios'
import util from './libs/util'
Vue.use(Router)
let  axios = new HttpRequest()
const router = new Router({
  mode: 'history',
  base: process.env.BASE_URL,
  routes: [
    {
      path: '*',
      name: '404',
      component:() => import('./views/404.vue'),
    },{
      path: '/login',
      name: 'login',
      component:() => import('./views/Login.vue'),
    },
    {
      path: '/',
      name: 'home',
      component: Home,
      children: [{
        path: '/app/list',
        name: 'appList',
        component:() => import('./views/security/TraceApp.vue'),
      },{
        path: '/app/detail',
        name: 'appDetail',
        component:() => import('./views/security/AppDetail.vue'),
      },{
        path: '/app/roles',
        name: 'approles',
        component:() => import('./views/security/AppRole.vue'),
      },{
        path: '/company/search',
        name: 'companySearch',
        component:() => import('./views/company/companyList.vue'),
      },{
        path: '/company/add',
        name: 'companyAdd',
        component:() => import('./views/company/companyAdd.vue'),
      },{
        path: '/company/edit',
        name: 'companyEdit',
        component:() => import('./views/company/companyAdd.vue'),
      },{
        path: '/about',
        name: 'about',
        component: () => import('./views/About.vue')
      },{
        path: '/category',
        name: 'category',
        component: () => import('./views/product/Category.vue')
      },{
        path: '/attribute/:pkid',
        name: 'attribute',
        component: () => import('./views/product/Attribute.vue')
      },{
        path: '/order/list',
        name: 'orderlist',
        component: () => import('./views/order/OrderList.vue')
      },{
        path: '/order/detail',
        name: 'orderdetail',
        component: () => import('./views/order/OrderDetail.vue')
      },{
        path: '/member/list',
        name: 'memberList',
        component: () => import('./views/member/MemberList.vue')
      },{
        path: '/activity/list',
        name: 'activityList',
        component: () => import('./views/market/ActivityList.vue')
      },{
        path: '/activity/product/list',
        name: 'aplist',
        component: () => import('./views/market/ActivityProductList.vue')
      },{
        path: '/seckill/list',
        name: 'seckillList',
        component: () => import('./views/market/SeckillList.vue')
      },{
        path: '/seckill/timeperiod/list',
        name: 'timeperiodList',
        component: () => import('./views/market/TimePeriodList.vue')
      },{
        path: '/seckill/timeperiod/product/list',
        name: 'stpl',
        component: () => import('./views/market/TimeperiodProductList.vue')
      },{
        path: '/coupon/list',
        name: 'couponList',
        component: () => import('./views/market/CouponList.vue')
      },{
        path: '/coupon/add',
        name: 'couponAdd',
        component: () => import('./views/market/CouponAdd.vue')
      },{
        path: '/security/user',
        name: 'securityUser',
        component: () => import('./views/security/SysUserList.vue')
      },{
        path: '/security/role',
        name: 'securityRole',
        component: () => import('./views/security/RoleList.vue')
      },{
        path: '/security/menu',
        name: 'securityMenu',
        component: () => import('./views/security/MenuList.vue')
      }]
    }
  ]
})

// 全局路由守卫
router.beforeEach((to, from, next) => {
  // to: Route: 即将要进入的目标 路由对象
  // from: Route: 当前导航正要离开的路由
  // next: Function: 一定要调用该方法来 resolve 这个钩子。执行效果依赖 next 方法的调用参数。
  const nextRoute = ['login'];//过滤的路由
  let isLogin = global.isLogin; // 是否登录
  // 未登录状态；当路由到nextRoute指定页时，跳转至login
  if (nextRoute.indexOf(to.name) >= 0) { //如果是过滤的路由直接放行
    next();
  }else{
    if(!util.checkLogin()){
       router.push({path:"/login"});
    }else{
      next();
    }
  }
 
 });

export default router;